package com.htfyun.ft.fragment.engineer;

import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Switch;
import android.widget.TextView;

import com.htfyun.ft.R;
import com.htfyun.ft.fragment.BaseFragment;
import com.htfyun.ft.utils.DebugLog;
import com.htfyun.hardware.WhiteListEnum;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;

/**
 * Created by sst on 2018/7/11.
 */

public abstract class FragmentWhiteListBase extends BaseFragment {

    private final static String TAG = FragmentWhiteListBase.class.getSimpleName();

    private TextView txtInfo;
    private Switch switchEnabled;
    private Button btnAdd;

    private ListView list;

    private WhiteListAdapter whiteListAdapter;

    @NonNull
    protected abstract WhiteListEnum getWhiteListEnum();
    protected abstract String getInfo();

    @Override
    protected int getLayoutRes() {
        return R.layout.fragment_engineer_white_list;
    }

    @Override
    protected void onInitView(@Nullable Bundle savedInstanceState) {

        txtInfo = (TextView) findViewById(R.id.txtInfo);
        txtInfo.setText(getInfo());

        switchEnabled = (Switch) findViewById(R.id.switchEnabled);
        btnAdd = (Button) findViewById(R.id.btnAdd);

        list = (ListView) findViewById(R.id.list);
        list.setEmptyView(findViewById(android.R.id.empty));

        whiteListAdapter = new WhiteListAdapter(mContext, -1);

        list.setAdapter(whiteListAdapter);
        updateWhiteList();

        switchEnabled.setChecked(getWhiteListEnum().isEnabled());
        switchEnabled.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                getWhiteListEnum().setEnabled(isChecked);
            }
        });

        btnAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showEditDialog();
            }
        });
    }

    private void showEditDialog() {
        final EditText editText = new EditText(mContext);
        AlertDialog.Builder inputDialog =
                new AlertDialog.Builder(mContext);
        inputDialog.setTitle(R.string.engineer_white_list_input).setView(editText);
        inputDialog.setPositiveButton(android.R.string.ok,
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        dialog.dismiss();
                        String pn = editText.getText().toString();
                        if (!TextUtils.isEmpty(pn)) {
                            addPackageNameIntoWhiteList(pn);
                        }
                    }
                })
        .setNegativeButton(android.R.string.cancel,
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                }).show();
    }

    private void addPackageNameIntoWhiteList(String pn) {
        List<String> pnList = new ArrayList<>();
        pnList.add(pn);
        getWhiteListEnum().addPackageNamesIntoWhiteList(pnList);
        updateWhiteList();
    }

    private void removePackageNameIntoWhiteList(String pn) {
        List<String> pnList = new ArrayList<>();
        pnList.add(pn);
        getWhiteListEnum().removePackageNamesFromWhiteList(pnList);
        updateWhiteList();
    }

    private void updateWhiteList() {

        List<String> wl = getWhiteListEnum().getWhiteListOfPackageName();
        whiteListAdapter.clear();
        whiteListAdapter.addAll(wl);
    }

    private final class WhiteListAdapter extends ArrayAdapter<String> {

        public WhiteListAdapter(@NonNull Context context, int resource) {
            super(context, resource);
        }

        @NonNull
        @Override
        public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
            ViewHolder holder = null;
            if (convertView != null) {
                holder = (ViewHolder) convertView.getTag();
            } else {
                holder = new ViewHolder();
                LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = inflater.inflate(R.layout.item_white_list, null);
                holder.txtPackageName = (TextView) convertView.findViewById(R.id.txtPackageName);
                holder.btnRemove = (Button) convertView.findViewById(R.id.btnRemove);

                convertView.setTag(holder);
            }

            String pn = getItem(position);
            holder.txtPackageName.setText(pn);
            holder.btnRemove.setTag(pn);
            holder.btnRemove.setOnClickListener(onClickListener);
            return convertView;
        }
    }

    private View.OnClickListener onClickListener =
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    DebugLog.e("remove package name = " + v.getTag());
                    if (v.getTag() instanceof String) {
                        removePackageNameIntoWhiteList((String) v.getTag());
                    }
                }
            };

    private final class ViewHolder {
        TextView txtPackageName;
        Button btnRemove;
    }

}
